// TOWN SCRIPT
//    Town 3: Proving Grounds - Third Level

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

// flags:
// 3, 27 = balanced pre-sets
// 3, 28 = presets all gone
// 3, 29 = generate new monsters

begintownscript;

variables;

short i, j, x, y, choice;

body;

beginstate INIT_STATE;
	turn_off_training(1);
	set_crime_tolerance(1);

	// check monsters...
	run_scenario_script(17);
	if (get_flag(3, 29) > 0) {
		// generate some more...
		set_flag(3, 29, 0);
		print_str_color("Generating monsters...", 3);
		j = get_ran(1, 14, 24);
		i = 0;
		while (i < j) {
			run_scenario_script(29);
			if (get_flag(100, 0) > 0) {
				run_scenario_script(26);
				place_monster(get_flag(100, 1), get_flag(100, 2), get_flag(100, 0), 0);
			}
			i = i + 1;
		}
		run_scenario_script(38);
	}
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
break;

beginstate 10;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a ladder leading up to the previous level.", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Climb up");
	choice = run_dialog(0);
	if (choice == 2) {
		// ladder back up...
		move_to_new_town(2, 35, 15);
	}
break;

beginstate 11;
	block_entry(1);
	x = char_loc_x(0);
	y = char_loc_y(0);
	if (y >= 40) {
		y = 4;
	}
	else {
		y = 43;
	}
	if (x == 5)
		x = 4;
	else if (x == 12)
		x = 13;
	else if (x == 15)
		x = 14;
	else if (x == 22)
		x = 23;
	else if (x == 25)
		x = 24;
	else if (x == 32)
		x = 33;
	else if (x == 35)
		x = 34;
	teleport_party(x, y, 1);
break;

beginstate 12;
	block_entry(1);
	x = char_loc_x(0);
	y = char_loc_y(0);
	if (x >= 40) {
		x = 4;
	}
	else {
		x = 42;
	}
	if (y == 5)
		y = 4;
	else if (y == 10)
		y = 11;
	else if (y == 13)
		y = 12;
	else if (y == 18)
		y = 19;
	else if (y == 21)
		y = 20;
	else if (y == 28)
		y = 29;
	else if (y == 31)
		y = 30;
	else if (y == 36)
		y = 37;
	else if (y == 39)
		y = 38;
	teleport_party(x, y, 1);
break;

beginstate 13;
	run_scenario_script(11);
break;

beginstate 14;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a ladder leading down into the maze.", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Climb down");
	choice = run_dialog(0);
	if (choice == 2) {
		// ladder down...
		move_to_new_town(4, 23, 6);
	}
break;

beginstate 15;
	reset_dialog();
	add_dialog_str(0, "There is an elevator here.", 0);
	add_dialog_str(1, "Use?", 0);
	add_dialog_choice(0, "No");
	add_dialog_choice(1, "Yes");
	choice = run_dialog(0);
	if (choice == 2) {
		block_entry(1);
		i = 0;
		while (i == 0) {
			get_text_response("Which level (A-D), C exits");
			check_text_response_match("A");
			if (got_text_match() > 0) {
				move_to_new_town(1, 25, 28);
				end();
			}
			check_text_response_match("B");
			if (got_text_match() > 0) {
				move_to_new_town(2, 24, 26);
				end();
			}
			check_text_response_match("C");
			if (got_text_match() > 0) {
				block_entry(0);
				end();
			}
			check_text_response_match("D");
			if (got_text_match() > 0) {
				move_to_new_town(4, 23, 26);
				end();
			}
		}
	}
break;

beginstate 16;
	message_dialog("A mosaic on the floor reads:", "Turn left.");
break;

beginstate 17;
	message_dialog("A mosaic on the floor reads:", "Turn right.");
break;

beginstate 18;
	message_dialog("A mosaic on the floor reads:", "Turn around.");
break;
